<?php

namespace App\Console\Commands\Transfer;

use App\Models\Ca\Visit;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class VisitCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'transfer:visit';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '迁移';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        DB::connection('S')->table('bs_visit')
            ->orderBy('id')
            ->where('status', '=', 1)
            ->select(
                'bs_visit.id',
                'bs_visit.booking_id as customer_id',
                'bs_visit.user_id',
                'bs_visit.content',
                'bs_visit.postil as supervisor_annotate',
            )
            ->selectRaw('FROM_UNIXTIME(bs_visit.create_time) as  created_at')
            ->selectRaw('FROM_UNIXTIME(bs_visit.update_time) as  updated_at')
            ->selectRaw('0 as hospital_id')
            ->chunk(1000, function ($data) {
                $is = [];
                foreach ($data as $datum) {
                    $is[] = (array)$datum;
                }
                Visit::query()->insert($is);
            });
    }
}